﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.GoodsForms
{
    public partial class GoodsAddForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.DictionaryDetailBll dictionaryDetailsBll = new BLL.DictionaryDetailBll();
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        BLL.GoodsTypeBll goodsTypeBll = new BLL.GoodsTypeBll();
        Action _InitGrid;
        int _operation;
        Good _goods;
        public GoodsAddForm(Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _operation = 1;
        }


        public GoodsAddForm(Good goods, Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _operation = 2;
            _goods = goods;
            this.textEdit_GoodsName.Text = _goods.GoodsName;
            this.lookUpEdit_GoodsType.EditValue = _goods.GoodsTypeID;
            this.textEdit_Unit.Text = _goods.Unit;
            this.textEdit_Remark.Text = _goods.Remark;
        }

        private void GoodsAddForm_Load(object sender, EventArgs e)
        {
            //初始化产品分类
            this.lookUpEdit_GoodsType.Properties.DataSource = toolBll.GetGoodsTypeList();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.lookUpEdit_GoodsType.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                lookUpEdit_GoodsType.Focus();
                return;
            }
            if (this.textEdit_GoodsName.Text.Trim() == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                textEdit_GoodsName.Focus();
                return;
            }

            if (_operation == 1)
            {
                Good goods = new Good();
                goods.GoodsID = Guid.NewGuid().ToString();
                goods.GoodsName = this.textEdit_GoodsName.Text.Trim();
                try
                {
                    goods.GoodsTypeID = Convert.ToString(this.lookUpEdit_GoodsType.EditValue);
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                }
                goods.Remark = this.textEdit_Remark.Text;
                goods.Unit = this.textEdit_Unit.Text;
                string res = CreateGoodAndStandard(goods);
                _InitGrid();
                XtraMessageBox.Show(res);
                this.Close();
            }
            else if (_operation == 2)
            {
                using (OJCB4Entities db = new OJCB4Entities())
                {
                    Good goods = db.Goods.Find(_goods.GoodsID);
                    goods.GoodsName = this.textEdit_GoodsName.Text.Trim();
                    try
                    {
                        goods.GoodsTypeID = Convert.ToString(this.lookUpEdit_GoodsType.EditValue);
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show(ex.Message);
                        return;
                    }
                    goods.Remark = this.textEdit_Remark.Text;
                    goods.Unit = this.textEdit_Unit.Text;
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        _InitGrid();
                        this.Close();
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                    }
                }
            }
        }

        /// <summary>
        /// 创建样品名称，同时创建相应的标准
        /// </summary>
        /// <param name="Good"></param>
        /// <returns></returns>
        public string CreateGoodAndStandard(Good Good)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                var GoodCount = db.Goods.Where(m => m.GoodsName == Good.GoodsName && m.GoodsTypeID == Good.GoodsTypeID).Count();
                if (GoodCount > 0)
                {
                    return "已经存在相同名称的样品了！";
                }
                GoodsType GoodType = db.GoodsTypes.Where(m => m.GoodsTypeID == Good.GoodsTypeID).First();
                if (GoodType.GoodsTypeName == "原材料")
                {
                    Good = db.Set<Good>().Add(Good);
                    List<StandardRaw> standardList = new List<StandardRaw>();
                    List<TestItem> RawTestItems = db.SysReportTypes.Where(m => m.SysReportTypeName == "原材料检验报告").First().TestItems.ToList();
                    foreach (var item in RawTestItems)
                    {
                        StandardRaw standard = new StandardRaw();
                        standard.StandardRawID = Guid.NewGuid().ToString();
                        standard.FormOrder = item.Order;
                        standard.Good = Good;
                        standard.MaxValue = null;
                        standard.MinValue = null;
                        standard.TestItemID = item.TestItemID;
                        standardList.Add(standard);
                    }
                    foreach (var standard in standardList)
                    {
                        int res = db.StandardRaws.Where(m => m.GoodsID == standard.GoodsID && m.TestItemID == standard.TestItemID).Count();
                        if (res == 0)
                        {
                            db.Set<StandardRaw>().Add(standard);
                        }
                    }

                    int count = db.SaveChanges();
                    if (count > 0)
                    {
                        return "新增原材料成功！";
                    }
                    else
                    {
                        return "新增原材料失败！";
                    }

                }
                else if (GoodType.GoodsTypeName == "产品")
                {
                    Good = db.Set<Good>().Add(Good);
                    List<SamplingPlace> samplingPlaceList = db.SamplingPlaces.Where(m => true).ToList();
                    List<Customer> customerList = db.Customers.Where(m => true).ToList();
                    List<TestItem> semiTestItemList = db.SysReportTypes.Where(m => m.SysReportTypeName == "过程检验报告").First().TestItems.ToList();
                    List<TestItem> productTestItemList = db.SysReportTypes.Where(m => m.SysReportTypeName == "出厂检验报告").First().TestItems.ToList();
                    List<StandardSemi> semiStandardList = new List<StandardSemi>();
                    List<StandardCustomer> custStandardList = new List<StandardCustomer>();
                    //创建过程标准
                    foreach (var semiTestItem in semiTestItemList)
                    {
                        foreach (var samplingPlace in samplingPlaceList)
                        {
                            StandardSemi standardSemi = new StandardSemi();
                            standardSemi.StandardSemiID = Guid.NewGuid().ToString();
                            standardSemi.FormOrder = semiTestItem.Order;
                            standardSemi.Good = Good;
                            standardSemi.MaxValue = null;
                            standardSemi.MinValue = null;
                            standardSemi.SamplingPlaceID = samplingPlace.SamplingPlaceID;
                            standardSemi.TestItemID = semiTestItem.TestItemID;
                            semiStandardList.Add(standardSemi);
                        }
                    }
                    //创建出厂报告标准
                    foreach (var productTest in productTestItemList)
                    {
                        foreach (var customer in customerList)
                        {
                            StandardCustomer standardCustomer = new StandardCustomer();
                            standardCustomer.StandardCustomerID = Guid.NewGuid().ToString();
                            standardCustomer.FormOrder = productTest.Order;
                            standardCustomer.Good = Good;
                            standardCustomer.MaxValue = null;
                            standardCustomer.MinValue = null;
                            standardCustomer.TestItemID = productTest.TestItemID;
                            standardCustomer.CustomerID = customer.CustomerID;
                            custStandardList.Add(standardCustomer);
                        }
                    }

                    //开始存储
                    foreach (var semiStandard in semiStandardList)
                    {
                        db.Set<StandardSemi>().Add(semiStandard);
                    }
                    //foreach (var mixStandard in mixStandardList)
                    //{
                    //    this.CurrentDBSession.Db.Set<StandardMix>().Add(mixStandard);
                    //}
                    foreach (var custStandard in custStandardList)
                    {
                        db.Set<StandardCustomer>().Add(custStandard);
                    }

                    int count = db.SaveChanges();
                    if (count > 0)
                    {
                        return "新增产品成功！";
                    }
                    else
                    {
                        return "新增产品失败！";
                    }
                }
                else
                {
                    return "未知错误";
                }
            }
        }
    }
}